package 算法.力扣;

import java.util.*;

/**
 * @author: 谢君臣
 * @create: 2020-09-10 20:25
 * @founction: XXXX
 */
public class MainNum {
    public static void main(String[] args) {
       int[] num = { 1,2,5,9,9,5};
        int i = majorityElement(num);
        System.out.println(i);
    }

    public static int majorityElement(int[] nums) {
        int key = 0, max = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int n = map.get(nums[i]) == null ? 0 : map.get(nums[i]);
            map.put(nums[i], ++n);
            if (n>max){
                max = n;
                key = nums[i];
            }
        }
        if (max>nums.length/2){
            return key;
        }else {
            return -1;
        }
    }
}
